// 计算字符串的长度，避免长度过长
const calString = (str, len) => {
    if (str.length < len) {
        return str;
    } else {
        return str.slice(0, len) + '..';
    }
}
// 格式化歌词
const formatLyricTime = (time) => { // 格式化歌词的时间 转换成 sss:ms
    const regMin = /.*:/;
    const regSec = /:.*\./;
    const regMs = /\./;

    const min = parseInt(time.match(regMin)[0].slice(0, 2));
    let sec = parseInt(time.match(regSec)[0].slice(1, 3));
    const ms = time.slice(time.match(regMs).index + 1, time.match(regMs).index + 3);
    if (min !== 0) {
        sec += min * 60;
    }
    return Number(sec + '.' + ms)
}
const calData = (date) => {
    return new Date(parseInt(date)).toLocaleString().replace(/:\d{1,2}$/,' ')
}
const calInteger = (obj) => {
    if (Number.isInteger(obj)) {
        return obj;
    } else {
        // 这里会被直接舍弃小数
        return parseInt(obj) + 1;
    }
}
// cookie
const cookieToJson = (cookie) => {
    if (!cookie) return {}
    let cookieArr = cookie.split(';')
    let obj = {}
    cookieArr.forEach((i) => {
        let arr = i.split('=')
        obj[arr[0]] = arr[1]
    })
    return obj
}
// 将毫秒格式化
const calMSToMinAndS = (ms) => {
    let mss = parseInt(ms);
    let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = parseInt((mss % (1000 * 60)) / 1000);
    if (hours == 0) {
        return minutes + " : " + seconds;
    }
    return hours + " : " + minutes + " : " + seconds;
}
const toCode = (str) => {
    //定义密钥，36个字母和数字
    let key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let l = key.length;  //获取密钥的长度
    let a = key.split("");  //把密钥字符串转换为字符数组
    let s = "",b, b1, b2, b3;  //定义临时变量
    for (let i = 0; i <str.length; i ++) {  //遍历字符串
        b = str.charCodeAt(i);  //逐个提取每个字符，并获取Unicode编码值
        b1 = b % l;  //求Unicode编码值得余数
        b = (b - b1) / l;  //求最大倍数
        b2 = b % l;  //求最大倍数的于是
        b = (b - b2) / l;  //求最大倍数
        b3 = b % l;  //求最大倍数的余数
        s += a[b3] + a[b2] + a[b1];  //根据余数值映射到密钥中对应下标位置的字符
    }
    return s;  //返回这些映射的字符
}
const fromCode = (str) => {
    //定义密钥，36个字母和数字
    let key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let l = key.length;  //获取密钥的长度
    let b, b1, b2, b3, d = 0, s;  //定义临时变量
    s = new Array(Math.floor(str.length / 3));  //计算加密字符串包含的字符数，并定义数组
    b = s.length;  //获取数组的长度
    for (let i = 0; i < b; i ++) {  //以数组的长度循环次数，遍历加密字符串
        b1 = key.indexOf(str.charAt(d));  //截取周期内第一个字符串，计算在密钥中的下标值
        d ++;
        b2 = key.indexOf(str.charAt(d));  //截取周期内第二个字符串，计算在密钥中的下标值
        d ++;
        b3 = key.indexOf(str.charAt(d));  //截取周期内第三个字符串，计算在密钥中的下标值
        d ++;
        s[i] = b1 * l * l + b2 * l + b3  //利用下标值，反推被加密字符的Unicode编码值
    }
    b = eval("String.fromCharCode(" + s.join(',') + ")");//用fromCharCode()算出字符串
    return b ;  //返回被解密的字符串
}
export {
    calString,
    formatLyricTime,
    calData,
    calInteger,
    calMSToMinAndS,
    toCode,
    fromCode,
    cookieToJson
}